﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
using ToolRental.Client.SMvvm.Admin.AdminService;

namespace ToolRental.Client.SMvvm.Admin
{
    public class BorrowServiceAgent : IBorrowServiceAgent
    {
        #region GetBorrowDeviceList
        public async Task<List<Borrow>> GetBorrowDeviceList(INotifyError notifier)
        {
            List<Borrow> borrowList = new List<Borrow>();
            try
            {
                AdminService.Borrow[] borrowsFromService;
                using (var asc = new AdminServiceClient())
                {
                    borrowsFromService = await asc.GetBorrowsAsync();
                }
                foreach (var b in borrowsFromService)
                {
                    borrowList.Add(new Borrow()
                        {
                            BrandName = b.BrandName,
                            Deadline = b.Deadline,
                            StartDate = b.StartDate,
                            StockNumber = b.StockNumber,
                            TypeName = b.TypeName,
                            UserName = b.UserName,
                            Id = b.Id
                        });
                }
            }
            catch (EndpointNotFoundException e)
            {
                notifier.ErrorOccured("A kiszolgáló nem található!", e);
            }

            return borrowList;
        }
        #endregion

        #region GetOrderDeviceList
        public async Task<List<Order>> GetOrderDeviceList(INotifyError notifier)
        {
            List<Order> orderList = new List<Order>();
            try
            {
                AdminService.Order[] ordersFromService;
                using (var asc = new AdminServiceClient())
                {
                    ordersFromService = await asc.GetOrdersAsync();
                }
                foreach (var o in ordersFromService)
                {
                    orderList.Add(new Order()
                        {
                            BrandName = o.BrandName,
                            StartDate = o.StartDate,
                            TypeName = o.TypeName,
                            UserName = o.UserName,
                            Id = o.Id,
                            DeviceTypeId = o.ToolTypeId
                        });
                }
            }
            catch (EndpointNotFoundException e)
            {
                notifier.ErrorOccured("A kiszolgáló nem található", e);
            }

            return orderList;
        }
        #endregion

        #region FinishBorrow
        public async void FinishBorrow(int borrowId, INotifyError notifier)
        {
            try
            {
                using (var asc = new AdminServiceClient())
                {
                    await asc.DeleteBorrowAsync(borrowId);
                }
            }
            catch (EndpointNotFoundException e)
            {
                notifier.ErrorOccured("A kiszolgáló nem található!", e);
            }
        }
        #endregion

        #region StartBorrow
        public async void StartBorrow(int orderId, int toolId, DateTime deadline, INotifyError notifier)
        {
            try
            {
                using (var asc = new AdminServiceClient())
                {
                    await asc.NewBorrowAsync(orderId, toolId, deadline);
                }
            }
            catch (EndpointNotFoundException e)
            {
            }
        }
        #endregion

        #region GetSelectedOrderDeviceList
        public async Task<List<Device>> GetSelectedOrderDeviceList(int deviceTypeId, INotifyError notifier)
        {
            List<Device> deviceList = new List<Device>();
            try
            {
                AdminService.Tool[] toolsFromService;
                using (var asc = new AdminServiceClient())
                {
                    toolsFromService = await asc.GetAvailableToolsAsync(deviceTypeId);
                }
                foreach (var tool in toolsFromService)
                {
                    deviceList.Add(new Device()
                        {
                            DeviceTypeId = tool.ToolTypeId,
                            Id = tool.Id,
                            StockNumber = tool.StockNumber
                        });
                }
            }
            catch (EndpointNotFoundException e)
            {
                notifier.ErrorOccured("A kiszolgáló nem található!", e);
            }

            return deviceList;
        }
        #endregion
    }
}
